<?php

class Auth extends Font_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->library('sauth');
		$this->set_theme('auth');
	}
	
	/*
	 * 注册
	 */
	public function register()
	{
		if (is_get())
		{
			$this->register_get();
		}
		else
		{
			$this->register_post();
		}
	}
	
	protected function register_get()
	{
		$this->display('register.tpl');
	}
	
	protected function register_post()
	{
		$username = post('username', TRUE);
		$password = post('password', TRUE);
		$rpassword = post('rpassword', TRUE);
		$email = post('email', TRUE);
		
		if ($password!=$rpassword)
		{
			exit('两次密码不一致');
		}
		
		if (FALSE===$this->sauth->register($username, $password, $email))
		{
			msg($this->sauth->error(), -1);
		}
		$this->assign('email', $email);
		$this->display('register_success.tpl');
	}
	
	/*
	 * 登录
	 */
	public function login()
	{
		if (is_get())
		{
			$this->login_get();
		}
		else
		{
			$this->login_post();
		}
	}
	
	protected function login_get()
	{
		$this->display('login.tpl');
	}
	
	protected function login_post()
	{
		$id = post('uid', TRUE);
		$password = post('password', TRUE);
		if (FALSE===$this->sauth->login($id, $password))
		{
			msg($this->sauth->error(), -1);
		}
	}
	
	/*
	 * 登出
	 */
	public function logout()
	{
		$this->sauth->logout();
		msg('成功注销', site_url('auth/login'));
	}
	
	/*
	 * 激活
	 */
	public function active()
	{
		$id = intval(segment(3));
		$active_key = segment(4);
		
		if (FALSE===$this->sauth->active($id, $active_key))
		{
			$this->assign('error', $this->sauth->error());
			$this->display('active_faliure.tpl');
		}
		
		$this->display('active_success.tpl');
	}
	
	/*
	 * 修改密码
	 */
	public function rpassword()
	{
		$id = intval(segment(3));
		$rpassword_key = segment(4);
	}
	
	/*
	 * 验证码
	 */
	public function verify()
	{
		$this->load->library('image');
		$config = array(
			'name' => 'verify',
			'line' => 0
		);
		$this->image->code($config);
	}

}